using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Pages_Admin_EditAccounts : System.Web.UI.Page
{
    Account account;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["UserName"] == null)
        {
            Response.Redirect("~/Pages_Admin/ManageAccounts/ViewAccount.aspx");
        }
        else if (!Page.IsPostBack)
        {
            string userName = Request.QueryString["UserName"];
            account = DALAccount.GetOneAccount(userName);
            if (account != null && account.UserName != "")
            {
                SetAllFields();
            }
            else
            {
                Response.Redirect("~/Pages_Admin/ManageAccounts/ViewAccount.aspx");
            }
        }
    }

    protected void SetAllFields()
    {
        try
        {
            txtUserName.Text = account.UserName;
            ViewState["Pwd"] = account.Password;
            txtPassword.Attributes.Add("value", ViewState["Pwd"].ToString());
            txtFullName.Text = account.Fullname;
            txtAddress.Text = account.Address;
            txtPhoneNumber.Text = account.PhoneNumber;
            txtEmail.Text = account.Email;
            ddlBranch.SelectedValue = account.PinCode;
            cboIsWorking.Checked = account.IsWorking;
        }
        catch
        {
            Response.Redirect("~/Errors/GeneralError.aspx");
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        if (Page.IsValid)
        {
            try
            {
                int result;
                result = DALAccount.UpdateAccount(txtUserName.Text, txtPassword.Text, txtFullName.Text, txtAddress.Text, txtEmail.Text, txtPhoneNumber.Text, ddlBranch.SelectedValue, cboIsWorking.Checked);
                if (result > 0) //Success
                {
                    lblMessage.Text = "Edited successfully";
                }
                else
                {
                    lblMessage.Text = "Error editing account";
                }
            }
            catch
            {
                lblMessage.Text = "Error editing account";
            }
        }
    }
}
